<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="房屋名称" prop="name">
      <el-input v-model="dataForm.name" placeholder="房屋名称"></el-input>
    </el-form-item>
    <el-form-item label="图片">
        <el-upload
          class="avatar-uploader"
          action="http://localhost:8070/base-fast/file/fileController/upload"
          :show-file-list="false"
          :on-success="handleAvatarSuccess">
          <img v-if="imageUrl" :src="imageUrl" class="avatar" style="width: 50px;height: 50px;">
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </el-form-item>
    <el-form-item label="面积" prop="area">
      <el-input v-model="dataForm.area" placeholder="面积"></el-input>
    </el-form-item>
    <el-form-item label="大小" prop="hSize">
      <el-input v-model="dataForm.hSize" placeholder="三室两厅"></el-input>
    </el-form-item>
    <el-form-item label="地址" prop="address">
      <el-input v-model="dataForm.address" placeholder="地址"></el-input>
    </el-form-item>

    <el-form-item label="租赁面积" prop="rentArea">
      <el-input v-model="dataForm.rentArea" placeholder="租赁面积"></el-input>
    </el-form-item>

    <el-form-item label="租赁单位" prop="rentPriceUnit">
      <el-input v-model="dataForm.rentPriceUnit" placeholder="租赁单位"></el-input>
    </el-form-item>

    <el-form-item label="租赁价格" prop="rentPriceListing">
      <el-input v-model="dataForm.rentPriceListing" placeholder="租赁价格"></el-input>
    </el-form-item>

    <el-form-item label="标签" prop="tags">
      <el-input v-model="dataForm.tags" placeholder="精装,集中供暖,随时看房"></el-input>
    </el-form-item>

    <el-form-item label="房屋类型" prop="typeId">
      <el-select v-model="dataForm.typeId" placeholder="请选择">
          <el-option
            v-for="item in types"
            :key="item.id"
            :label="item.name"
            :value="item.id">
          </el-option>
        </el-select>
    </el-form-item>

    <el-form-item label="省份" prop="provinceId">
      <el-select v-model="provinceId" placeholder="请选择">
          <el-option
            v-for="item in provinces"
            :key="item.id"
            :label="item.name"
            :value="item.id">
          </el-option>
        </el-select>
    </el-form-item>
    <el-form-item label="城市" prop="cityId">
      <el-select v-model="cityId" placeholder="请选择">
          <el-option
            v-for="item in citys"
            :key="item.id"
            :label="item.name"
            :value="item.id">
          </el-option>
        </el-select>
    </el-form-item>
    <el-form-item label="地区" prop="areaId">
      <el-select v-model="dataForm.areaId" placeholder="请选择">
          <el-option
            v-for="item in areas"
            :key="item.id"
            :label="item.name"
            :value="item.id">
          </el-option>
        </el-select>
    </el-form-item>
    <el-form-item label="描述信息" prop="note">
      <el-input v-model="dataForm.note" type="textarea" placeholder="描述信息"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        imageUrl:'',
        types:[],
        provinces:[],
        citys:[],
        areas:[],
        provinceId:'',
        cityId:'',
        areaId:'',
        dataForm: {
          id: 0,
          name: '',
          img: '',
          area: '',
          note: '',
          typeId: '',
          address:'',
          hSize:'',
          rentArea:'',
          rentPriceUnit:'',
          rentPriceListing:'',
          tags:'',
          areaId:''
        },
        dataRule: {
          name: [
            { required: true, message: '房屋名称不能为空', trigger: 'blur' }
          ],
          img: [
            { required: true, message: '图片地址不能为空', trigger: 'blur' }
          ],
          area: [
            { required: true, message: '面积不能为空', trigger: 'blur' }
          ],
          note: [
            { required: true, message: '描述信息不能为空', trigger: 'blur' }
          ],
          typeId: [
            { required: true, message: '房屋类型不能为空', trigger: 'blur' }
          ]
        }
      }
    },watch:{
      provinceId(newVal,oldVal){

        if(newVal !== ''){
            // 根据 省份id查询对应的城市信息
            this.$http({
              url: this.$http.adornUrl(`/house/tcity/queryByProvinceId/`+newVal),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.citys = data.citys

              }
            })
        }
      },cityId(newVal,oldVal){
        console.log(newVal,oldVal)
        if(newVal !== ''){
          // 根据 城市id查询对应的地址信息
          this.$http({
              url: this.$http.adornUrl(`/house/tarea/queryByCityId/`+newVal),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.areas = data.areas

              }
            })
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/house/thouse/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.name = data.tHouse.name
                this.dataForm.img = data.tHouse.img
                this.dataForm.area = data.tHouse.area
                this.dataForm.note = data.tHouse.note
                this.dataForm.status = data.tHouse.status
                this.dataForm.userId = data.tHouse.userId
                this.dataForm.address = data.tHouse.address
                this.dataForm.hSize = data.tHouse.hSize
                this.dataForm.createTime = data.tHouse.createTime
                this.dataForm.typeId = data.tHouse.typeId
                this.dataForm.rentArea =  data.tHouse.rentArea
                this.dataForm.rentPriceUnit =  data.tHouse.rentPriceUnit
                this.dataForm.rentPriceListing =  data.tHouse.rentPriceListing
                this.dataForm.tags = data.tHouse.tags
                this.dataForm.areaId = data.tHouse.areaId


                this.types = data.types
                this.provinces = data.provinces
              }
            })
          }else{
            this.$http({
              url: this.$http.adornUrl(`/house/thouse/info/0`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.provinces = data.provinces
                this.types = data.types
              }
            })


          }// 图片上传成功的操作
        })
      },
    handleAvatarSuccess(res){
      this.dataForm.img = res;
      this.imageUrl = "http://localhost:8070/base-fast/file/fileController/download?fileName="+res;
    },handleDownload(path){
      // 处理文件的下载操作
      window.location.href = "http://localhost:8070/base-fast/file/fileController/downloadFile?fileName="+path;

    },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/house/thouse/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'name': this.dataForm.name,
                'img': this.dataForm.img,
                'area': this.dataForm.area,
                'note': this.dataForm.note,
                'status': this.dataForm.status,
                'hsize': this.dataForm.hSize,
                'address': this.dataForm.address,
                'userId': this.dataForm.userId,
                'createTime': this.dataForm.createTime,
                'typeId': this.dataForm.typeId,
                'rentArea':this.dataForm.rentArea,
                'rentPriceUnit':this.dataForm.rentPriceUnit,
                'rentPriceListing':this.dataForm.rentPriceListing,
                'tags':this.dataForm.tags,
                'areaId':this.dataForm.areaId

              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
